const app = getApp()
const myrequest = (url, options) => {
   return new Promise((resolve, reject) => {
       wx.showLoading({
         title: '请求中',
       })
       wx.request({
           url:app.globalData.baselink+url,
           method: "POST",
           data: options,
           header:{
            'content-type': 'application/x-www-form-urlencoded',
            'uid':getApp().globalData.user.uid
          },
           success(request) {       
               console.log(request)
               if(request.data.code==200){
                    resolve(request.data)     
               }else{
                wx.showToast({
                    title: request.data.msg,
                    icon:"none"
                  })
                  reject(request.data)
               }            
           },
           fail(error) {
               wx.showToast({
                 title: error.data.msg,
                 icon:"none"
               })
               reject(error.data)
           },
           complete(){
               wx.hideLoading()
           }
       })
   })
}
const myuploadfile = (url,files,options) => {
    return new Promise((resolve, reject) => {
        options.uid = getApp().globalData.user.uid
        console.log(url,files,options)
        wx.showLoading({
          title: '请求中',
        })
        wx.uploadFile({
          filePath: files,
          name: 'file',
          url: getApp().globalData.baselink+url,
          formData:options,
          success(res){
              
              res = JSON.parse(res.data)
            if(res.code==200){
                resolve(res)
            }else{
                reject(res)
            }
          },
          fail(res){
            reject(res)
          },
          complete(){
              wx.hideLoading()
          }
        })
    })
}

module.exports = {
    myrequest,
    myuploadfile
}
